/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui.report;

import easybooking.ReportClientController;
import easybooking.data.Airline;
import easybooking.data.Customer;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

/**
 *
 * @author Tim Schröder
 */
public class RCCustomerReportDialog extends JDialog {

    private ReportClientController rcc;
    private JPanel dialogPanel;
    private JPanel buttonPanel;
    private JButton btnOk;
    private JButton btnCancel;
    private JLabel lblAril;
    private JTextField txfAirline = new JTextField();
    private JButton btnAirlineAll;
    private JLabel lblCustomer;
    private JTextField txfCustomer = new JTextField();
    private Airline airl;
    private Date fromDate;
    private Date toDate;
    private Customer editableCustomer;
    private Customer c;
    private JLabel lblFromDate;
    private JTextField txfFromDate = new JTextField();
    private JButton btnFromDate;
    private JLabel lblToDate;
    private JTextField txfToDate = new JTextField();
    private JButton btnToDate = null;
    private JButton btnDateAll = null;

    /**
     * constructor for the RCCustomerReportDialog
     * @param rcif, the ReportClientController
     */
    public RCCustomerReportDialog(ReportClientController rcif) {
        rcc = rcif;
    }

    /**
     * reads out the airline
     * @return a Airline Object which is selected
     */
    public Airline getAirline() {
        return this.airl;
    }

    /**
     * sets the selected or all airlines from which the customer report shall be created
     * @param airl is the selected airline
     */
    public void setAirline(Airline airl) {
        if (airl != null) {
            try {
                this.airl = airl;
                txfAirline.setText(airl.getName());
            } catch (RemoteException ex) {
                Logger.getLogger(RCCustomerReportDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            this.airl = null;
            txfAirline.setText("All Airlines");
        }
    }

    /**
     * reads out the selected date
     * @return the selected date Object
     */
    public Date getFromDate() {
        return this.fromDate;
    }

    /**
     * sets the date from which the report shall be created
     * @param date is the salected date
     */
    public void setFromDate(Date date) {
        if (date != null) {
            try {
                this.fromDate = date;
                txfFromDate.setText(date.toString());
            } catch (Exception ex) {
                Logger.getLogger(RCCustomerReportDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            this.fromDate = null;
            txfFromDate.setText("All");
        }
    }

    /**
     * reads out the selected date
     * @return the selected date Object
     */
    public Date getToDate() {
        return this.toDate;
    }

    /**
     * sets the date to which the report shall be created
     * @param date is the salected date
     */
    public void setToDate(Date date) {
        if (date != null) {
            try {
                this.toDate = date;
                txfToDate.setText(date.toString());
            } catch (Exception ex) {
                Logger.getLogger(RCCustomerReportDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            this.toDate = null;
            txfToDate.setText("All");
        }
    }

    /**
     * sets the atributs of the customer into the text fields
     * @param c is the customer object which atributs get instert into the text fields
     */
    public void setTextFields(Customer c) {
        try {
            txfCustomer.setText(editableCustomer.getFirstname() + " " + editableCustomer.getLastname());
        } catch (RemoteException ex) {
            Logger.getLogger(RCCustomerReportDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //shows the dialog when needed
    /**
     * calls the generateComponents() method to create and show the CustomerDialog
     * @param c is the type object which should be edit
     */
    public void createAndShowDialog(Customer c) {
        editableCustomer = c;
        generateComponents();
        setTextFields(c);
        this.setResizable(true);
        this.setTitle("Create Customer Report");
        this.setModal(true); // deaktivates input to other dialogs
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
        this.toFront();
    }

    /**
     * calls the generateComponents() method to create and show the CustomerDialog
     */
    public void createAndShowDialog() {
        generateComponents();
        btnOk.setName("createFlight");
        this.setResizable(true);
        this.setTitle("Create Flight");
        this.setModal(true); // deaktivates input to other dialogs
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
        this.toFront();
    }

    // Preperes the dialog
    /**
     * Generates all components to create and display the GUI
     */
    private void generateComponents() {

        // Dialog Settings

        dialogPanel = new JPanel();


        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }



        dialogPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        lblCustomer = new JLabel("Customer:");
        addComponent(dialogPanel, gbl, lblCustomer, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);
        txfCustomer = new JTextField(20);
        txfCustomer.setEditable(false);
        this.setTextFields(editableCustomer);
        addComponent(dialogPanel, gbl, txfCustomer, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 0, 1, 1, 0, 0);

        lblAril = new JLabel("Airline:");
        addComponent(dialogPanel, gbl, lblAril, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 1, 1, 1, 0, 0);
        txfAirline = new JTextField(20);
        txfAirline.setEditable(false);
        addComponent(dialogPanel, gbl, txfAirline, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 1, 1, 1, 0, 0);

        btnAirlineAll = new JButton("Select");
        btnAirlineAll.setActionCommand("CustomerReportDialogAirlineSelect");
        btnAirlineAll.addActionListener(rcc);
        addComponent(dialogPanel, gbl, btnAirlineAll, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 2, 1, 1, 1, 0, 0);

        btnAirlineAll = new JButton("All");
        btnAirlineAll.setActionCommand("CustomerReportDialogAirlineAll");
        btnAirlineAll.addActionListener(rcc);
        addComponent(dialogPanel, gbl, btnAirlineAll, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 3, 1, 1, 1, 0, 0);

        lblFromDate = new JLabel("From Date:");
        addComponent(dialogPanel, gbl, lblFromDate, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 2, 1, 1, 0, 0);
        txfFromDate = new JTextField(20);
        txfFromDate.setEditable(false);
        addComponent(dialogPanel, gbl, txfFromDate, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 2, 1, 1, 0, 0);

        btnFromDate = new JButton("Select");
        btnFromDate.setActionCommand("CustomerReportDialogDateSelect");
        btnFromDate.setName("FromDate");
        btnFromDate.addActionListener(rcc);
        addComponent(dialogPanel, gbl, btnFromDate, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 2, 2, 1, 1, 0, 0);

        btnDateAll = new JButton("All");
        btnDateAll.setActionCommand("CustomerReportDialogDateAll");
        btnDateAll.addActionListener(rcc);
        addComponent(dialogPanel, gbl, btnDateAll, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 3, 2, 1, 1, 0, 0);

        lblToDate = new JLabel("To Date:");
        addComponent(dialogPanel, gbl, lblToDate, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 3, 1, 1, 0, 0);
        txfToDate = new JTextField(20);
        txfToDate.setEditable(false);
        addComponent(dialogPanel, gbl, txfToDate, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 3, 1, 1, 0, 0);

        btnToDate = new JButton("Select");
        btnToDate.setActionCommand("CustomerReportDialogDateSelect");
        btnToDate.setName("ToDate");
        btnToDate.addActionListener(rcc);
        addComponent(dialogPanel, gbl, btnToDate, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 2, 3, 1, 1, 0, 0);

        // adds to buttonPanel
        addComponent(dialogPanel, gbl, this.createButtonPanel(), GridBagConstraints.NONE, GridBagConstraints.EAST, 0, 6, 2, 1, 0, 0);

        // sets die dialogPanel as contentPane
        this.setContentPane(dialogPanel);


    }

    // Creates the button panel
    /**
     * Creates the buttons to commit with ok or cancel
     * @return a JPanel with 2 buttons
     */
    private JPanel createButtonPanel() {

        buttonPanel = new JPanel();

        btnOk = new JButton("OK");
        btnCancel = new JButton("Cancel");

        // Sets the buttons the same size
        btnOk.setPreferredSize(btnCancel.getPreferredSize());

        // Registrate action listeners
        btnOk.setActionCommand("CreateCustomerReportDialogOk");
        btnOk.addActionListener(rcc);

        btnCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        // Adds the buttons to the buttonPanel
        buttonPanel.add(btnOk);
        buttonPanel.add(btnCancel);
        return buttonPanel;
    }

    /**
     * defines the layout of the GUI
     * @param cont container to which the component is added
     * @param gbl GridBagLayout object
     * @param c compontent which is added to the container
     * @param fill how to use free space in the cell around the component
     * @param anchor where to fix an align the component
     * @param x x position in the grid layout
     * @param y y position in the grid layout
     * @param width number of columns the element should spread
     * @param hight number of rows the element should spread
     * @param weightx handels the horizontal free space
     * @param weighty handels the vertical free space
     */
    private static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);


    }

    public void setCustomer(Customer c) {
        this.c = c;
    }

    public Customer getCustomer() {
        return this.c;
    }
}
